var pages = 1;
var num = 0;
$(function() {
	
	if(getCookieValue("token")==""){
		window.location.href = 'l-userlogin.html'
	}
	
	
	storeMyOrder();
	$('.top-one').on('tap',function(){
		window.location.href = 'g-center.html?business='+CD.geturl().business;
	});
	
	$('#buyer').on('tap',function(){
		window.location.href = 'v-buyer.html?business='+CD.geturl().business;
	});

	//头部遍历
	headStore(function(data) {
		$('.store-head').html(template('headStoreTemplate', data));

		//店铺跳转
		$('#myStoreUserUuid').on('tap', function() {
			window.location.href = "v-shop.html?business=" + CD.geturl().business;
		});

	});

	$('.myshop-product').on('tap', function() {
		$(this).addClass('myshopcur').siblings().removeClass('myshopcur');
		$('.myshop-order-list').css({
			"display": "none"
		});
		$('.myproduct').css({
			"display": "block"
		});
		$('.addproduct-addcategory').css({
			"display": "block"
		});
		$('#mui-scroll-wrapper').css({
			"display": "none"
		});
		$('#mui-scroll-wrapper-product').css({
			"display": "block"
		});

	});
	$('.myshop-order').on('tap', function() {
		$(this).addClass('myshopcur').siblings().removeClass('myshopcur');
		$('.myshop-order-list').css({
			"display": "block"
		});
		$('.myproduct').css({
			"display": "none"
		});
		$('.addproduct-addcategory').css({
			"display": "none"
		});
		$('#mui-scroll-wrapper').css({
			"display": "block"
		});
		$('#mui-scroll-wrapper-product').css({
			"display": "none"
		});

	});

	//	新建分类
	$('#mystore-newcategory').on('tap', function() {
		mui.prompt('新建分类', '请输入分类', '', function(e) {
			if(e.index == 1) {
				console.log(e.value);
				var typeName = e.value;
				if(typeName.length) {
					storeNewCategory({
						typeName: typeName,
						userUuid:CD.geturl().business
					}, function() {
						
						console.log('添加分类成功');
						//分类遍历
						myProductCategory();
					});
				} else {
					mui.toast('请输入名字');
				}
			} else {
				console.log(2);
			}
		});
	});

	//商品预约遍历
	productOrder({
		"pages": 1,
		"userUuid":CD.geturl().business,
	}, function(data) {
		console.log(data, 88888);
		$('#myshop-order-list').html(template('productOrderTemplate', data));
		//二维码图片显示隐藏及更换
		myStoreWx();
	});

	//分类遍历
	myProductCategory();

})
//商品预约
var productOrder = function(params, callback) {
	$.ajax({
		type: "get",
		url: window.address + "/order/user",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: 'json',
		success: function(data) {
			console.log(data)
			callback && callback(data);
		}
	});
}

//卖家头部信息
var headStore = function(callback) {
	$.ajax({
		type: "post",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/user/findBayUserUuid",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: {
			"userUuid":CD.geturl().business
		},
		success: function(data) {
			console.log(data,123);
			callback && callback(data);
		}
	});
}

//卖家分类
var storeCategory = function(callback) {
	$.ajax({
		type: 'get',
		url: window.address + '/goods/findBayUuidAndTypes',
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: {
			userUuid: CD.geturl().business
		},
		dataType: 'json',
		success: function(data) {
			console.log(data, 888);
			callback && callback(data);
		}
	});
}

//商品预约上拉加载
var storeMyOrder = function() {
	mui.init({
		pullRefresh: {
			container: "#mui-scroll-wrapper",
			up: {
				callback: function() {
					var that = this;
					pages++;
					productOrder({
						"pages": pages
					}, function(data) {
						$('#myshop-order-list').append(template('productOrderTemplate', data));
						myStoreWx();
						if(data.bayUserUuid.length) {
							that.endPullupToRefresh();
						} else {
							that.endPullupToRefresh(true);
						};
					})
				}
			}
		}
	});
}

//商品预约操作
var myStoreWx = function() {
	//商品预约中的二维码显示
	$('.mystore-open').on('tap', function() {
		var imgUrl = $(this).attr('data-urll');
		console.log(imgUrl);
		$('.mybuyer-wecht-show').css({
			"display": "block"
		});
		//更换图片链接
		$('#mybuyer-change img').attr({
			"src": imgUrl
		});
	});
	$('#mybuyer-close').on('tap', function() {
		$('.mybuyer-wecht-show').css({
			"display": "none"
		});
	});

	//电话
	$('.mybuyer-phone').on('tap', function() {
		var thisPhone = $(this).attr('data-phone');
		console.log(thisPhone)
		$('.mybuyer-phone-show').stop().fadeIn();
		$('.mybuyer-phone-show').children().children('span').html(thisPhone);
	});
	$('.mybuyer-phone-show').click(function() {
		$('.mybuyer-phone-show').stop().fadeOut();
	});

	//删除
	$('.trash-delete').on('tap', function() {
		var trashId = $(this).attr('data-id');
		var trashiP = $(this).parents('.myshop-order-list-detail');
		var uuId = $(this).attr('data-uuid');
		console.log(trashId, 888);
		$('.buy-delete').stop().fadeIn();
		$('#buy-delete-close').on('tap', function() {
			$('.buy-delete').stop().fadeOut();
		});
		$('#buy-delete-open').on('tap', function() {
			storeTrashDelete({
				"goodsId": trashId,
				"clientUuid": uuId,
				"userUuid":CD.geturl().business
			}, function() {
				mui.toast('删除成功！')
			});
			$('.buy-delete').stop().fadeOut();
			trashiP.stop().fadeOut();
		});
	});
	
	//新建分类
	$('.createNewPru').on('tap',function(){
		window.location.href = 'v-store-newbox.html?business='+CD.geturl().business
	});
	
	
}

//商品预约删除
var storeTrashDelete = function(params, callback) {
	$.ajax({
		type: 'get',
		url: window.address + '/order/deleteUser',
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: 'json',
		success: function(data) {
			console.log(data, 888);
			callback && callback(data);
		}
	});
}

//新建分类
var storeNewCategory = function(params, callback) {
	$.ajax({
		type: 'get',
		url: window.address + '/TbType/insert',
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: 'json',
		success: function(data) {
			console.log(data, 888);
			callback && callback(data);
		}
	});
}

//我的商品分类遍历
var myProductCategory = function() {
	
	storeCategory(function(data) {
		$('.myproduct').html(template('catecoryTemplate', data));
		//分类跳转
		$('.myproduct-category-num').on('click', function() {
			var id = $(this).parent().attr('data-catecoryid');
			console.log(id, 88);
			window.location.href = 'v-store-mystorecategory.html?id=' + id + "&business=" + CD.geturl().business;
		});
		//	编辑分类
		$('.mystore-edit').on('tap', function() {
			var typeId = $(this).parent().siblings().attr('data-id');
			mui.prompt('修改分类', '请输入分类', '', function(e) {
				if(e.index == 1) {
					var reviseNum = e.value;
					if(reviseNum.length) {
						myproductRevise({
							id: typeId,
							typeName: reviseNum
						}, function(data) {
							console.log(typeId, 1212112);
							myProductCategory();
						});
					} else {
						mui.toast('请输入名字')
					}
				} else {
					console.log(2);
				}
			});
		});
		//删除分类
		$('.mystore-trash').on('tap', function() {
			var typeId = $(this).parent().siblings().attr('data-id');
			console.log(typeId);
			mui.confirm('确定删除此分类吗?', '商品分类', function(e) {
				if(e.index == 1) {
					console.log(1);
					myproductDelete({
						typeId: typeId
					}, function(data) {
						console.log(typeId, 1212112);
						myProductCategory();
					});
				} else {
					console.log(2)
				}
			});
			return false;
		});
		

	});
}

//我的商品分类删除
var myproductDelete = function(params, callback) {
	$.ajax({
		type: 'get',
		url: window.address + '/TbType/deleteTypeId',
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: 'json',
		success: function(data) {
			console.log(data, 888);
			callback && callback(data);
		}
	});
}

//我的商品修改分类
var myproductRevise = function(params, callback) {
	$.ajax({
		type: 'get',
		url: window.address + '/TbType/updateBayId',
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: 'json',
		success: function(data) {
			console.log(data, 888);
			callback && callback(data);
		}
	});
}